package com.sailor.controller;

import com.sailor.annotation.Sailor;
import com.sailor.common.CommonResponse;
import com.sailor.event.OrderEvent;
import com.sailor.pojo.Person;
import com.sailor.pojo.User;
import com.sailor.service.PersonService;
import com.sailor.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@Slf4j
public class MyController {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private UserService userService;

    @Autowired
    private PersonService personService;

    @RequestMapping({"/", "/index"})
    public String toIndex(Model model) {
        model.addAttribute("msg", "hello,shior");
        return "index";
    }

    @RequiresPermissions(value = "1211.fly")
    @RequestMapping("/user/add")
    public String add() {

        return "user/add";
    }

    @RequestMapping("/user/update")
    public String update() {

        return "user/update";
    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/login")
    public String login(String username, String password, Model model) {
        // 获取当前用户
        Subject subject = SecurityUtils.getSubject();
        // 封装用户登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        // 执行登录方法
        try {
            subject.login(token);
            applicationContext.publishEvent(new OrderEvent(subject, "已登录"));
            System.out.println("调用成功");
            return "index";
        } catch (UnknownAccountException e) {// 用户名不存在
            model.addAttribute("msg", "用户名错误");
            return "login";
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg", "密码错误");
            return "login";
        }

    }

    @RequestMapping("/unauthorized")
    public String unauthorized() {
        return "您没有查看该页面的权限!";
    }

    @PostMapping("/add_user")
    public Integer addUser(@RequestBody User user) {
        userService.addUser(user);
        return user.getId();
    }

    @GetMapping("/get_user_by_name")
    public User getUserByName(@RequestParam("name") String name) {
        return userService.getUserByname(name);
    }

    @GetMapping("/frequent clicks")
    @Sailor()
    public CommonResponse frequentClicks(@RequestParam("name") String name) {
        User userByname = userService.getUserByname(name);
        return CommonResponse.builder().data(userByname).build();
    }

    @PostMapping("/valid")
    public CommonResponse valid(@Valid @RequestBody User user) {
        System.out.println(user.getId());
        System.out.println(user.getIdList());
        return CommonResponse.SUCCESS;
    }

    @GetMapping("/addTest")
    public Integer addTest() {
        Person person = new Person();
        person.setName("test");
        Integer id = personService.addPerson(person);
        return id;
    }

    @GetMapping("/logTest")
    public void logTest() {
        log.info("info消息");
        log.error("error消息");
        log.warn("warn消息");
        log.debug("debug消息");
    }


}
